keepalived 的启动脚本和安装脚本 的结合配置文件详解

keepalived 的启动脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived
. /etc/init.d/functions
# Source configuration file (we set KEEPALIVED_OPTIONS there)
#. /etc/sysconfig/keepalived
KEEP_CONF="/export/servers/keepalived-1.2.13/conf/keepalived.conf"
KEEP_SBIN="/export/servers/keepalived-1.2.13/sbin/keepalived"
KEEP_PID="/var/run/keepalived.pid "
RETVAL=0
prog="keepalived"
start() {
echo -n $"Starting $prog: "
daemon $KEEP_SBIN --use-file=$KEEP_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $KEEP_PID $KEEP_SBIN -TERM
RETVAL=$?
echo
#[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}
reload() {
echo -n $"Reloading $prog: "
#killproc keepalived -1
killproc -p $KEEP_PID $KEEP_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
status)
status keepalived
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
=======
#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived
# Source function library
. /etc/init.d/functions
# Source configuration file (we set KEEPALIVED_OPTIONS there)
#. /etc/sysconfig/keepalived
KEEP_CONF="/export/servers/keepalived-1.2.13/conf/keepalived.conf"
KEEP_SBIN="/export/servers/keepalived-1.2.13/sbin/keepalived"
KEEP_PID="/var/run/keepalived.pid "
RETVAL=0
prog="keepalived"
start() {
echo -n $"Starting $prog: "
daemon $KEEP_SBIN --use-file=$KEEP_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $KEEP_PID $KEEP_SBIN -TERM
RETVAL=$?
echo
#[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}
reload() {
echo -n $"Reloading $prog: "
#killproc keepalived -1
killproc -p $KEEP_PID $KEEP_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
status)
status keepalived
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
>>>>>>> .r3360

keepalived 的安装脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
role=$1
weight=$2
vip=$3
#routerid=`ifconfig |grep Bcast|cut -d: -f2|awk '{print $1}'|awk -F. '{print $4}'`
routerid=$4
interface=`ifconfig | awk -F" " '{print $1}' | grep -v lo | grep -v ^\s*$`
#vrrd_id=`ifconfig |grep "inet addr"|grep -v "127.0.0.1"|awk -F: '{print $2}'|awk '{print $1}'|awk -F. '{print $4}'`
keep_dir=/export/servers/keepalived-1.2.13
conf_dir=$keep_dir/conf
pid_dir=$keep_dir/pid
sh_dir=/export/sh
WHO=`whoami`
function help()
{
echo -e "Install Tips:#Such as master安装 sh $0 MASTER 100 vip routerid # BACKUP安装 sh $0 BACKUP [LESS THEN 100] vip routerid"
}
if [ $# -eq 0 ]
then
help
exit 1
fi
########检查网络,安装popt包
echo -e "\033[;37;32mCheckNetwork\033[0m"
if ! ping -c 1 -w 1 172.22.197.62 > /dev/null; then
echo -e "\033[;37;31mPlease Check Network Before Setup\033[0m";
exit 1;
else
echo -e "\033[;37;32mOK\033[0m";
fi
echo -e "\033[;37;32mStarting Yum\033[0m"
yum -y install popt-devel openssl openssl-devel make openssl-devel popt popt-devel libnl-devel kernel-devel
echo -e "\033[;37;32mYum Install Done\033[0m"
####download and install keepalived-1.2.13.tar.gz
cd /usr/local/src/keepalived
if [ ! -e keepalived-1.2.13.tar.gz ]
then
wget http://172.22.197.62/CentOS/app/keepalived-1.2.13.tar.gz
tar xzf keepalived-1.2.13.tar.gz
else
tar xzf keepalived-1.2.13.tar.gz
fi
rpm -ivh popt-static-1.13-7.el6.x86_64.rpm
cd keepalived-1.2.13
./configure --prefix=/export/servers/keepalived-1.2.13 --sysconfdir=/export/servers/keepalived-1.2.13/etc/ --bindir=/export/servers/keepalived-1.2.13/bin/ --with-kernel-dir=/usr/src/linux/
make && make install
if [ $? -eq 0 ]
then
echo "make success"
else
echo "make error"
exit 1
fi
cp /export/servers/keepalived-1.2.13/sbin/keepalived /usr/sbin/
cp /export/servers/keepalived-1.2.13/etc/sysconfig/keepalived /etc/sysconfig/
cp ../keepalived /etc/init.d/
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived
chkconfig keepalived on
echo -e "\033[;37;32m创建keepalive配置文件路径\033[0m"
mkdir -p $conf_dir
#######添加keepalive配置文件
cat > $conf_dir/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
notification_email {
lihuiyw@jd.com
}
notification_email_from lihuiyw@jd.com
smtp_server mail.jd.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_alive
{
script "/export/sh/check_nginx_alive.sh"
# check every 2 seconds
interval 2
# if failed, decrease 10 of the priority
weight -10
# require 2 failures for failures
fail 2
# require 1 sucesses for ok
rise 1
}
vrrp_instance VIP_${routerid}
{
state $role
interface $interface
virtual_router_id $routerid
priority $weight
advert_int 2
garp_master_delay 10
smtp_alert
authentication
{
auth_type PASS
auth_pass 123456
}
virtual_ipaddress
{
${vip}/24
}
track_interface
{
$interface
}
track_script
{
check_alive
}
}
EOF
###########监控keepalived脚本、脚本需要放到同keepalived配置文件指定路径
echo -e "\033[;37;32m创建脚本路径\033[0m"
mkdir -p $sh_dir $pid_dir
cat > $sh_dir/check_nginx_alive.sh << EOF
#!/bin/bash
#监控脚本功能概述:
#首先检查进程中的nginx进程数目,如果不存在(即为0),则表示nginx未开启,然后开启nginx。
#3秒后重新检查nginx进程数,若仍为0,则表示nginx无法正常启动,此时强制停止keepalived进程,让虚拟ip切换到backup服务器上
## 如果没有nginx进程,即值为零
#echo \`pgrep -l nginx\`
#echo \`ps -ef | grep nginx\`
if [ \`pgrep "^nginx" | wc -l\` -eq 0 ]
then
service nginx start
sleep 1
if [ \`pgrep "^nginx" | wc -l\` -eq 0 ]
then
## 则结束 keepalived 进程,使得服务器切换到BACKUP服务器上
service keepalived stop
fi
fi
EOF
echo -e "\033[;37;32m赋可执行权限\033[0m"
chown -R admin.admin $conf_dir $keep_dir $sh_dir
#chmod 777 $sh_dir/check_nginx_alive.sh
/export/servers/keepalived-1.2.13/sbin/keepalived --use-file=/export/servers/keepalived-1.2.13/conf/keepalived.conf
>/home/install_keepalived.log
if [ $? -eq 0 ]
then
echo "keepalived install done"
fi